package com.hsnn.medstgmini.util;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.hsnn.medstgmini.model.SysPubHoliday;
import com.hsnn.medstgmini.service.SysPubHolidayManager;


/**
 * @category 公共假日工具类
 * @author 韩守松
 * @date 2015年11月24日
 */
public class PubHolidayUtil {
	
	private static SysPubHolidayManager sysPubHolidayManager;
	
	/**
	 * @category 
	 * @author 韩守松 获取指定工作日后的日期（国假）
	 * @date   2015年11月24日
	 * @param  @param date
	 * @param  @param num
	 * @param  @return
	 */
	public static SysPubHoliday getWorkDay(Date date,Integer num){
		Calendar calendar=Calendar.getInstance();  
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//让日期加1 
		date = calendar.getTime();
		SysPubHoliday sysPubHoliday = new SysPubHoliday();
		for(int i=1;i<num;i++){
			if(sysPubHolidayManager == null){
				WebApplicationContext wac = ContextLoader
						.getCurrentWebApplicationContext();
				sysPubHolidayManager = wac.getBean(SysPubHolidayManager.class);
			}
			sysPubHoliday = sysPubHolidayManager.getWorkToHolidayDay(date);
			if(sysPubHoliday == null){
				if(0==getWeekOfDate(date) || 6==getWeekOfDate(date)){
					sysPubHoliday = sysPubHolidayManager.getHolidayToWorkDay(date);
					if(sysPubHoliday != null){
						 calendar.setTime(date);
						 calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//让日期加1 
						 date = calendar.getTime();
						 continue;
					}else{
						 num += 1;
						 calendar.setTime(date);
						 calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//让日期加2 
						 date = calendar.getTime();
						 continue;
					}
				}
				 calendar.setTime(date);
				 calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//让日期加1
				 date = calendar.getTime();
			}else{
				 num += 1;
				 calendar.setTime(date);
				 calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//让日期加2 
				 date = calendar.getTime();
			}
			
		}
		SysPubHoliday sysPubHolidayResult = new SysPubHoliday();
		sysPubHolidayResult.setDay(date);
		return sysPubHolidayResult;
	}
	
	/**
	 * @category 获取工作日转假日的日期
	 * @author 韩守松
	 * @date   2015年11月24日
	 * @param  @param date
	 * @param  @return
	 */
	public static SysPubHoliday getWorkToHolidayDay(Date date){
		return sysPubHolidayManager.getWorkToHolidayDay(date);
	}
	
	/**
	 * @category 获取假日转工作日的日期
	 * @author 韩守松
	 * @date   2015年11月24日
	 * @param  @param date
	 * @param  @return
	 */
	public static SysPubHoliday getHolidayToWorkDay(Date date){
		return sysPubHolidayManager.getHolidayToWorkDay(date);
	}
	
	/** * 获取指定日期是星期几
	  * 参数为null时表示获取当前日期是星期几
	  * @param date
	  * @return
	*/
	public static Integer getWeekOfDate(Date date) { 
		Map<Integer,String> map = new HashMap<Integer, String>();
		map.put(0, "星期日");
		map.put(1, "星期一");
		map.put(2, "星期二");
		map.put(3, "星期三");
		map.put(4, "星期四");
		map.put(5, "星期五");
		map.put(6, "星期六");
	    Calendar calendar = Calendar.getInstance();      
	    if(date != null){        
	         calendar.setTime(date);      
	    }        
	    Integer w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0){        
	        w = 0;      
	    }      
	    return w;    
	}
}
